package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.oos_report.OosReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.IOosReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;

import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;

@Slf4j
@Service
public class OosReportServiceImpl implements IOosReportService {


    public List<OosReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("A.INPUT_TIMESTAMP,");
        sqlVo.appendSql("b_1.data_desc as FAB_ID_FK,");
        sqlVo.appendSql("A.P_OPE_ID_FK,");
        sqlVo.appendSql("B.DATA_GROUP_FK,");
        sqlVo.appendSql("b_2.tool_dsc as P_TOOL_ID_FK,");
        sqlVo.appendSql("A.CHART_VALUE,");
        sqlVo.appendSql("A.CHART_USL_FK,");
        sqlVo.appendSql("A.CHART_LSL_FK");
        sqlVo.appendSql("FROM spc_data.spc_ret_chart_data A");
        sqlVo.appendSql("LEFT JOIN spc_ret_chart_info B ON A.COL_TYP_FK = B.COL_TYP_FK and A.GRP_NO_FK = B.GRP_NO_FK AND A.CHART_NO_FK = B.CHART_NO_FK and A.CHART_TYP_FK = B.CHART_TYP_FK");
        sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc from icim_jn.bis_data where data_cate = 'FBID') b_1 ON A.fab_id_fk = b_1.data_ext");
        sqlVo.appendSql("LEFT JOIN (select tool_id,tool_dsc from icim_jn.bis_tool ) b_2 ON A.p_tool_id_fk = b_2.tool_id");
        sqlVo.appendSql("WHERE 1=1");
        sqlVo.appendSql("AND CHART_SPEC_RLT_FK != 'N'");
        if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo.appendSql("AND A.FAB_ID_FK in" + jointInSql(conditions.getFab_id()));
        if (StringUtils.isNotEmpty(conditions.getOpe_id())) sqlVo.appendSql("AND A.P_OPE_ID_FK in" + jointInSql(conditions.getOpe_id()));
        if (StringUtils.isNotEmpty(conditions.getShift_id())) sqlVo.appendSql("AND A.SHIFT_ID_FK in" + jointInSql(conditions.getShift_id()));
        if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND A.LINE_ID_FK in" + jointInSql(conditions.getBay_id()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND A.INPUT_TIMESTAMP >= ? ", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND A.INPUT_TIMESTAMP <= ? ", conditions.getEnd_time());
        sqlVo.appendSql("ORDER BY A.INPUT_TIMESTAMP");

        return mySql_QueryRunner_QueryListBySql(sqlVo, OosReportGridOut.class);
    }



}
